<?php

namespace app\mall\controller\admin;


use app\mall\enum\FreightTypeEnum;
use suframe\thinkAdmin\traits\CURDController;
use suframe\thinkAdmin\ui\UITable;
use app\mall\model\MallFreightTemplate as MallFreightTemplateModel;
use think\facade\Db;
use think\facade\View;

/**
 * 运费模板
 */
class MallFreightTemplate extends Base
{
    use CURDController;
    protected $urlPre = '/mall/admin/mallFreightTemplate/';

    private function getManageModel()
    {
        return MallFreightTemplateModel::class;
    }

    private function ajaxSearch()
    {
        $rs = $this->parseSearchWhere($this->getManageModel());
        $rs->append(['type_text', 'enable_text']);
        return json_return($rs);
    }

    /**
     * @param \suframe\form\Form $form
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \ReflectionException
     */
    private function getFormSetting($form)
    {
        $form->setRuleByClass(\app\mall\ui\form\MallFreightTemplateForm::class);
    }

    /**
     * @param UITable $table
     */
    private function getTableSetting($table)
    {
        $table->createByClass(\app\mall\ui\table\MallFreightTemplateTable::class);
        $table->setButtons('add', ['title' => '增加', 'url' => $this->urlABuild('update')]);
        $table->setEditOps($this->urlA('update'), ['id']);
        $table->setDeleteOps($this->urlA('delete'), ['id']);
    }

    /**
     * @return string|\think\response\Json
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \ReflectionException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \Exception
     */
    public function update()
    {
        $this->curlInit();
        $this->CURLAllowActions('update');
        /** @var MallFreightTemplateModel $info */
        $info = $this->getUpdateInfo();
        if ($this->request->isAjax() && $this->request->post()) {
            $post = $this->request->post();
            if (!$info) {
                $class = $this->getManageModel();
                $info = new $class;
            }
            Db::startTrans();
            try {
                $post = $this->beforeSave($info, $post);
                $rs = $info->save($post);
                Db::commit();
                return $this->handleResponse($rs);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                throw $e;
            }
        }
        $this->getThinkAdminViewLayout();
        $type = FreightTypeEnum::toZhArray();
        $typeUnit = FreightTypeEnum::toUnitZhArray();
        $info = $info ? $info->toJson() : json_encode([
            'type' => 'piece',
            'area_conf' => [],
            'area_special_conf' => []
        ], JSON_UNESCAPED_UNICODE);
        View::assign('data', $info);
        View::assign('type', json_encode($type));
        View::assign('typeUnit', json_encode($typeUnit));

        return View::fetch('/mallFreightTemplate/update');
    }

}